﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using WebInterface;
using WebServices;

namespace IOCTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            try
            {
                IServiceCollection services = new ServiceCollection();
                services.AddTransient<ITestServiceA, TestServiceA>();

                services.AddSingleton<ITestServiceB, TestServiceB>();

                services.AddScoped<ITestServiceC, TestServiceC>();

                services.AddTransient<ITestServiceD, TestServiceD>();
                services.AddTransient<ITestServiceE, TestServiceE>();

                var container = services.BuildServiceProvider();
                {
                    System.Console.WriteLine("*****************1**************");
                    var A1 = container.GetService<ITestServiceA>();
                    var A2 = container.GetService<ITestServiceA>();
                    System.Console.WriteLine(A1.Equals(A2));
                }
                {
                    System.Console.WriteLine("*****************2**************");
                    var B1 = container.GetService<ITestServiceB>();
                    var B2 = container.GetService<ITestServiceB>();
                    System.Console.WriteLine(B1.Equals(B2));
                }
                {
                    System.Console.WriteLine("*****************3**************");
                    var C1 = container.GetService<ITestServiceC>();
                    var C2 = container.GetService<ITestServiceC>();
                    System.Console.WriteLine(C1.Equals(C2));
                }
                {
                    System.Console.WriteLine("*****************4**************");
                    var C1 = container.GetService<ITestServiceC>();
                    var C2 = container.GetService<ITestServiceC>();
                    var C3 = container.CreateScope().ServiceProvider.GetService<ITestServiceC>();
                    var C4 = container.CreateScope().ServiceProvider.GetService<ITestServiceC>();

                    ITestServiceC C5 = null;
                    ITestServiceC C6 = null;
                    ITestServiceC C7 = null;

                    Task.Run(() =>
                    {
                        C5 = container.GetService<ITestServiceC>();
                    });
                    Task.Run(() =>
                    {
                        C6 = container.GetService<ITestServiceC>();
                    }).ContinueWith(t=>
                    {
                        C7 = container.GetService<ITestServiceC>();
                    });

                    System.Console.WriteLine(C1.Equals(C2));
                    System.Console.WriteLine(C1.Equals(C3));
                    System.Console.WriteLine(C1.Equals(C4));
                    System.Console.WriteLine(C3.Equals(C4));
                    System.Console.WriteLine(C3.Equals(C5));
                    System.Console.WriteLine(C3.Equals(C6));
                    System.Console.WriteLine(C3.Equals(C7));
                    System.Console.WriteLine(C5.Equals(C6));
                    System.Console.WriteLine(C5.Equals(C7));
                    System.Console.WriteLine(C6.Equals(C7));
                }


            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}
